package com.example.bmanliku;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class IndexActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView LeakageType, LeakProcess, LeakTime, TreatmentResults, UnitType, SceneOfOccurrence, LeakingForm;
    private SearchView SearchBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        initView();
        /*
        ***SearchView的处理
         */
        SearchBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            //输入完点击后进行搜索
            public boolean onQueryTextSubmit(String s) {
                String content=s;
                OkHttpClient client = new OkHttpClient();
                FormBody body = new FormBody.Builder()
                        .add("jiansuotext", content)
                        .build();
                final Request request = new Request.Builder()
                        .url("http://192.168.1.7:8000/jiansuo/")
                        .post(body)
                        .build();
                client.newCall(request).enqueue(new Callback()
                {

                    @Override
                    public void onFailure(Call call, IOException e) {
                        Toast.makeText(IndexActivity.this, "网络异常，请稍后再试！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException
                    {

                        final String SearchText=response.body().string();
                        Gson gson=new Gson();
                        Search_cs search_cs=gson.fromJson(SearchText,Search_cs.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("TagList", search_cs.getSearchText());
                        Intent intent=new Intent(IndexActivity.this,ListActivity.class);
                        intent.putExtras(bundle);
                        finish();
                        startActivity(intent);
                    }
                });
                return false;
            }
            //输入改变后进行搜索
          @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }

        });


    }


    private void initView() {
        //找到这个动态的线性布局
        LeakageType = findViewById(R.id.LeakageType);
        LeakProcess = findViewById(R.id.LeakProcess);
        LeakTime = findViewById(R.id.LeakTime);
        TreatmentResults = findViewById(R.id.TreatmentResults);
        UnitType = findViewById(R.id.UnitType);
        SceneOfOccurrence = findViewById(R.id.SceneOfOccurrence);
        LeakingForm = findViewById(R.id.LeakingForm);
        SearchBar=findViewById(R.id.SearchBar);
        SearchBar.setSubmitButtonEnabled(true);//设置显示提交按钮
        SetOnClickListener();
    }

    private void SetOnClickListener() {
        LeakageType.setOnClickListener(this);
        LeakProcess.setOnClickListener(this);
        LeakTime.setOnClickListener(this);
        TreatmentResults.setOnClickListener(this);
        UnitType.setOnClickListener(this);
        SceneOfOccurrence.setOnClickListener(this);
        LeakingForm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.LeakageType:
                Intent intent1 = new Intent(IndexActivity.this, IndexActivity1.class);
                startActivity(intent1);
                break;
            case R.id.LeakProcess:
                Intent intent2 = new Intent(IndexActivity.this, IndexActivity2.class);
                startActivity(intent2);
                break;
            case R.id.LeakTime:
                Intent intent3 = new Intent(IndexActivity.this, IndexActivity3.class);
                startActivity(intent3);
                break;

            case R.id.TreatmentResults:
                Intent intent4 = new Intent(IndexActivity.this, IndexActivity4.class);
                startActivity(intent4);
                break;

            case R.id.UnitType:
                Intent intent5 = new Intent(IndexActivity.this, IndexActivity5.class);
                startActivity(intent5);
                break;

            case R.id.SceneOfOccurrence:
                Intent intent6 = new Intent(IndexActivity.this, IndexActivity6.class);
                startActivity(intent6);
                break;

            case R.id.LeakingForm:
                Intent intent7 = new Intent(IndexActivity.this, IndexActivity7.class);
                startActivity(intent7);
                break;

        }


    }

    public void CreateClick(View view) {
        Intent intent = new Intent(IndexActivity.this, CreateActivity.class);
        startActivity(intent);
    }


}